<?php

namespace App\Console\Commands;

use App\Models\AdMediaDocking;
use App\Models\AdPlan;
use Illuminate\Console\Command;

class Datacache extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'yq:datacache';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成缓存数据';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        ini_set('memory_limit', '1024M');
        $this->datacache();
    }

    public function datacache()
    {
        $list = [];
        $data = AdPlan::get();
        foreach ($data as $val) {
            $list[$val->id] = $val->media_docking_id;
        }
        $file = storage_path('datacache/plan_docking.dat');
        file_put_contents($file, json_encode($list));

        $list = [];
        $data = AdMediaDocking::get();
        foreach ($data as $val) {
            $list[$val->id] = $val->toArray();
        }
        $file = storage_path('datacache/docking.dat');
        file_put_contents($file, json_encode($list));
    }
}
